<template>
    <div class="tags" v-if="showTags">
      <div class="jiantouleft">
        <button class="el-icon-arrow-left" @click="leftmove"></button>
      </div>
      <ul class="tags-ul" :style="{width:tagsList.length * 100 + 'px','margin-left': num * 100 + 'px'}">
        <li
          class="tags-li"
          v-for="(item, index) in tagsList"
          :class="{ active: isActive(item.path) }"
          :key="index"
        >
          <router-link :to="item.path" class="tags-li-title">
            {{ item.title }}
          </router-link>
          <span class="tags-li-icon" v-if="item.title!=='首页'" @click="closeTags(index)">
            <i class="el-icon-close"></i>
          </span>
          <span class="tags-li-icon" v-else></span>
        </li>
      </ul>
      <div class="jiantouright">
        <button class="el-icon-arrow-right" @click="rightmove"></button>
      </div>
      <div class="tags-close-box">
        <el-dropdown @command="handleTags">
          <el-button class="closebtn" size="mini" type="primary">
            <i class="el-icon-arrow-down el-icon-menu"></i>
          </el-button>
          <el-dropdown-menu size="small" slot="dropdown">
            <el-dropdown-item command="all">关闭所有</el-dropdown-item>
            <el-dropdown-item command="other">关闭其他</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
</template>
<script>
import bus from "../../utils/bus";
export default {
  name: "Tags",
  data() {
    return {
      tagsList: [{path:'/home',title:'首页'}],
      num:0,
    };
  },
  methods: {
     leftmove() {
        if (this.num < 0) {
            this.num += 1;
        }
    },
    rightmove() {
        if (this.num > -(this.tagsList.length + this.num)) {
            this.num -= 1;
        }
    },
    isActive(path) {
      return path == this.$route.fullPath;
    },
    // 关闭单个标签
    closeTags(index) {
      const delItem = this.tagsList.splice(index, 1)[0];
      const item = this.tagsList[index]
        ? this.tagsList[index]
        : this.tagsList[index - 1];
      if (item) {
        delItem.path == this.$route.fullPath && this.$router.push(item.path);
      } else {
        this.$router.push("/home");
      }
    },
    // 关闭其他标签
    closeOther(){
        const curItem = this.tagsList.filter(item => {
            return item.path === this.$route.fullPath;
        })
        this.tagsList = curItem;
    },
    // 关闭全部标签
    closeAll() {
      this.tagsList = [{path:'/home',title:'首页'}];
      this.$router.push("/home");
    },
    // 设置标签
    setTags(route) {
      const isExist = this.tagsList.some((item) => {
        return item.path == route.fullPath;
      });
      !isExist &&
        this.tagsList.push({
          title: route.meta.title,
          path: route.fullPath,
        });
      bus.$emit("tags", this.tagsList);
    },
    handleTags(command) {
     command === 'other' ? this.closeOther() : this.closeAll();
    },
  },
  //计算属性
  computed: {
    showTags() {
      return this.tagsList.length > 0;
    },
  },
  //监听
  watch: {
    $route(newValue, oldValue) {
      this.setTags(newValue);
    },
  },
  mounted() {
    this.setTags(this.$route);
    // 监听关闭当前页面的标签页
      bus.$on('close_current_tags', () => {
          for (let i = 0, len = this.tagsList.length; i < len; i++) {
              const item = this.tagsList[i];
              if(item.path === this.$route.fullPath){
                  if(i < len - 1){
                      this.$router.push(this.tagsList[i+1].path);
                  }else if(i > 0){
                      this.$router.push(this.tagsList[i-1].path);
                  }else{
                      this.$router.push('/home');
                  }
                  this.tagsList.splice(i, 1);
                  break;
              }
          }
      })
  },
};
</script>
<style>
::-webkit-scrollbar {
  display: none;
  width: 0 !important;
}
.tags {
  position: relative;
  display: flex;
  height: 40px;
  line-height: 40px;
  box-shadow: 0 1px 1px #ccc;
  width: 100%;
  overflow: hidden;
}
.tags-ul {
  display: flex;
  white-space:nowrap;
  box-sizing: border-box;
  overflow-x: auto;
  width: 100%;
  height: 100%;
}
.tags-li {
  display:inline;
  width: 64px;
  text-align: center;
  z-index: 50;
  border-radius: 3px;
  padding:0 3px;
  overflow: hidden;
  margin: 1.5px;
  cursor: pointer;
  height: 34px;
  line-height: 32px;
  border: 1px solid #4098ff;
  background: #fff;
  vertical-align: middle;
  color: #666;
  -webkit-transition: all .3s ease-in;
  -moz-transition: all .3s ease-in;
  transition: all .3s ease-in;
}
.tags-li.active {
  background: #4098ff;
  color: #fff;
}
.tags-li.active .tags-li-title {
  color: #fff;
}
.tags-li-title {
  text-decoration: none;
  display: inline-block;
  color: #000;
  max-width: 80px;
  vertical-align: middle;
  font-size: 12px;
}
.tags-li-icon {
  display: inline-block;
  vertical-align: middle;
  font-size: 0.5rem;
  margin-left: 3px;
}
.tags-close-box {
  position:fixed;
  right: 0;
  z-index: 100;
  width: 44px;
  height: 40px;
  cursor: pointer;
  background: #ccc;
}
.closebtn{
  font-size: 12px;
  z-index: 100;
  position: relative;
  left: 4px;
  width: 90%;
  height: 30px;
}
.jiantouleft{
  position: relative;
  z-index: 100;
  width: 20px;
  height: 40px;
  cursor: pointer;
}
.jiantouright{
  margin-left:25%;
  color:black;
  z-index: 100;
  position:fixed;
  right: 44px;
  width: 20px;
  height: 40px;
  cursor: pointer;
}
.jiantouleft .el-icon-arrow-left{
  height: 40px;
  background: #ccc;
  border: none;
  width: 20px;
}
.jiantouright .el-icon-arrow-right{
  height: 40px;
  background: #ccc;
  border: none;
  width: 20px;
}
</style>
